package com.znjc.web.controller.device;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.znjc.common.annotation.Log;
import com.znjc.common.core.controller.BaseController;
import com.znjc.common.core.domain.AjaxResult;
import com.znjc.common.core.domain.entity.SysDept;
import com.znjc.common.core.page.TableDataInfo;
import com.znjc.common.enums.BusinessType;
import com.znjc.common.utils.StringUtils;
import com.znjc.common.utils.poi.ExcelUtil;
import com.znjc.device.domain.DeviceDevice;
import com.znjc.device.domain.IotDataCheck;
import com.znjc.device.service.IDeviceDeviceService;
import com.znjc.device.service.IIotDataCheckService;
import com.znjc.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 【校验】Controller
 *
 * @author zengpc
 * @date 2023-12-04
 */
@RestController
@RequestMapping("/device/check")
public class IotDataCheckController extends BaseController {
    @Autowired
    private IIotDataCheckService iotDataCheckService;
    @Autowired
    private IDeviceDeviceService deviceDeviceService;
    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询校验管理列表
     */
    @PreAuthorize("@ss.hasPermi('device:check:list')")
    @GetMapping("/list")
    public TableDataInfo list(IotDataCheck iotDataCheck) {
        startPage();
        LambdaQueryWrapper<IotDataCheck> wrapper = getWrapper(iotDataCheck);
        List<IotDataCheck> list = iotDataCheckService.list(wrapper);
        return getDataTable(packList(list));
    }

    /**
     * 导出校验管理列表
     */
    @PreAuthorize("@ss.hasPermi('device:check:export')")
    @Log(title = "校验管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotDataCheck iotDataCheck) {
        LambdaQueryWrapper<IotDataCheck> wrapper = getWrapper(iotDataCheck);
        List<IotDataCheck> list = iotDataCheckService.list(wrapper);
        ExcelUtil<IotDataCheck> util = new ExcelUtil<IotDataCheck>(IotDataCheck.class);
        util.exportExcel(response, packList(list), "校验管理数据");
    }

    /**
     * 获取校验管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:check:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(iotDataCheckService.getById(id));
    }

    /**
     * 新增校验管理
     */
    @PreAuthorize("@ss.hasPermi('device:check:add')")
    @Log(title = "校验管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IotDataCheck iotDataCheck) {
        return toAjax(iotDataCheckService.save(iotDataCheck));
    }

    /**
     * 修改校验管理
     */
    @PreAuthorize("@ss.hasPermi('device:check:edit')")
    @Log(title = "校验管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotDataCheck iotDataCheck) {
        return toAjax(iotDataCheckService.updateById(iotDataCheck));
    }

    /**
     * 删除校验管理
     */
    @PreAuthorize("@ss.hasPermi('device:check:remove')")
    @Log(title = "校验管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iotDataCheckService.removeByIds(new ArrayList<>(Arrays.asList(ids))));
    }

    /**
     * 查询公共方法
     *
     * @param entity
     * @return
     */
    private LambdaQueryWrapper getWrapper(IotDataCheck entity) {
        LambdaQueryWrapper<IotDataCheck> wrapper = new LambdaQueryWrapper<>();
        if (null != entity) {
            if (StringUtils.isNotEmpty(entity.getCheckName())) {
                wrapper.like(IotDataCheck::getCheckName, entity.getCheckName());
            }
        }
        return wrapper;
    }

    /**
     * 结果封装
     *
     * @param list
     * @return
     */
    private List<IotDataCheck> packList(List<IotDataCheck> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(entity -> {
                if (null != entity.getDeptId()) {
                    SysDept dept = deptService.selectDeptById(entity.getDeptId());
                    if (null != dept) {
                        entity.setDeptName(dept.getDeptName());
                    }
                }
            });
        }
        return list;
    }
}
